package com.bcbim.api_web.pipeManagement.controller;

import com.alibaba.fastjson.JSONArray;
import com.bcbim.api_web.sys.interceptor.TranslateHandle;
import com.bcbim.api_web.sys.redis.MyRedisService;
import com.bcbim.authentication_interface.util.AuthUtil;
import com.bcbim.common.response.annotation.Translate;
import com.bcbim.common.response.vo.ReturnMsg;
import com.bcbim.common.response.vo.ReturnPageMsg;
import com.bcbim.common.sys.Constant;
import com.bcbim.common.sys.ParamException;
import com.bcbim.common.sys.ReturnStatus;
import com.bcbim.common.util.ExcelUtil;
import com.bcbim.common.util.MyListUtil;
import com.bcbim.common.util.ParamCheck;
import com.bcbim.common.util.ZipUtils;
import com.bcbim.smart_water_interface.file.service.FileService;
import com.bcbim.smart_water_interface.file.vo.FileVO;
import com.bcbim.smart_water_interface.line.service.PipelineLineService;
import com.bcbim.smart_water_interface.pipeManagement.vo.PipeListVO;
import com.bcbim.smart_water_interface.pipeline.VO.PipelineLineVO;
import com.bcbim.smart_water_interface.pipeline.VO.PipelinePointVO;
import com.bcbim.smart_water_interface.point.service.PipelinePointService;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 管网信息管理下载controller
 *
 * @author wxc
 * @version V1.0
 * @date 2021/8/19
 **/
@RestController
@RequestMapping("/pipeManagementDownload")
public class PipeManagementDownloadController {

    @Reference(protocol = "dubbo", group = "pipelineLine", version = "1.0.0")
    private PipelineLineService pipelineLineServiceImpl;
    @Reference(protocol = "dubbo", group = "pipelinePoint", version = "1.0.0")
    private PipelinePointService pipelinePointServiceImpl;
    @Reference(protocol = "dubbo", group = "pipeFile", version = "1.0.0")
    private FileService fileServiceImpl;
    @Autowired
    private MyRedisService myReidsService;

    @Value("${application.upload-path}")
    private String UPLOAD_PATH;

//
//    /**
//     * 添加下载集选（单个）
//     *
//     * @param number 集选数据
//     * @return com.bcbim.common.response.vo.ReturnMsg
//     * @param： Code 集选编号
//     * @param： value 集选类型
//     * @author wxc
//     * @date 2021/10/25
//     */
//    @PostMapping("/insertOneDownload")
//    public ReturnMsg insertOneDownload(Code number) {
//        try {
//            ParamCheck.stringNotEmpty(number.getCode(), 100);
//            ParamCheck.stringNotEmpty(number.getValue(), 1);
//        } catch (ParamException e) {
//            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
//        }
//        ArrayList<Code> list = new ArrayList<>();
//        list.add(number);
//        boolean b = addDownload(list);
//        return b ? ReturnMsg.consSuccess("添加下载集成功！") : ReturnMsg.consFail("添加下载集失败！");
//    }
//
//    /**
//     * 添加下载集选（添加的是类型的所有集选的数据）
//     *
//     * @param type 管网类型
//     * @return com.bcbim.common.response.vo.ReturnMsg
//     * @author wxc
//     * @date 2021/10/25
//     */
//    @PostMapping("/insertListDownload")
//    public ReturnMsg insertListDownload(String type) {
//        try {
//            ParamCheck.stringCanBeEmpty(type, 1);
//        } catch (ParamException e) {
//            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
//        }
//        // 取redis中数据集合
//        String pointToken = AuthUtil.getPointToken();
//        String lineToken = AuthUtil.getLineToken();
//        List<Code> allList = new ArrayList<Code>();
//        // 管点
//        if (Constant.PIPELINE_POINT.equals(type) || Constant.PIPELINE_ALL.equals(type)) {
//            JSONArray jsonArray = (JSONArray) myReidsService.get(pointToken);
//            List<PipeListVO> cacheList = jsonArray == null ? null : jsonArray.toJavaList(PipeListVO.class);
//            // 如果集选中存在数据,遍历添加到allList
//            if (ObjectUtils.isNotEmpty(cacheList)) {
//                cacheList.forEach(obj -> {
//                    Code code = new Code();
//                    code.setCode(obj.getCode()).setValue(Constant.PIPELINE_POINT);
//                    allList.add(code);
//                });
//            }
//        }
//        // 管线
//        if (Constant.PIPELINE_LINE.equals(type) || Constant.PIPELINE_ALL.equals(type)) {
//            JSONArray jsonArray = (JSONArray) myReidsService.get(lineToken);
//            List<PipeListVO> cacheList = jsonArray == null ? null : jsonArray.toJavaList(PipeListVO.class);
//            // 如果集选中存在数据,遍历添加到allList
//            if (ObjectUtils.isNotEmpty(cacheList)) {
//                cacheList.forEach(obj -> {
//                    Code code = new Code();
//                    code.setCode(obj.getCode()).setValue(Constant.PIPELINE_LINE);
//                    allList.add(code);
//                });
//            }
//        }
//        if (ObjectUtils.isEmpty(allList)) {
//            return ReturnMsg.consFail("集选中暂无数据！");
//        }
//        addDownload(allList);
//        return ReturnMsg.consSuccess(Constant.SAVE_SUCCESS);
//    }

    /**
     * 删除单个下载集
     *
     * @param id
     * @return com.bcbim.common.response.vo.ReturnMsg
     * @author wxc
     * @date 2021/10/25
     */
    @RequestMapping("/deleteDownload")
    public ReturnMsg deleteDownload(String id, String type) {
        String pipeLineDownload = AuthUtil.getPipeLineDownloadToken();
        String pipePointDownload = AuthUtil.getPipePointDownloadToken();
        try {
            ParamCheck.idCheck(id);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        if (type.equals(Constant.PIPELINE_POINT)) {
            JSONArray pointJson = (JSONArray) myReidsService.get(pipePointDownload);
            if (ObjectUtils.isEmpty(pointJson)) {
                return ReturnMsg.consFail("下载集数据为空，删除失败！");
            }
            List<PipeListVO> pointList = pointJson.toJavaList(PipeListVO.class);
            pointList = pointList.stream().filter(listVO -> !listVO.getId().equals(id)).collect(Collectors.toList());

            myReidsService.delete(pipePointDownload);
            myReidsService.set(pipePointDownload, pointList, Constant.PERMISSION_EXPIRATION_TIME);
        }
        if (type.equals(Constant.PIPELINE_LINE)) {
            JSONArray lineJson = (JSONArray) myReidsService.get(pipeLineDownload);
            List<PipeListVO> lineList = lineJson.toJavaList(PipeListVO.class);
            lineList = lineList.stream().filter(pipeListVO -> !pipeListVO.getId().equals(id)).collect(Collectors.toList());

            myReidsService.delete(pipeLineDownload);
            myReidsService.set(pipeLineDownload, lineList, Constant.PERMISSION_EXPIRATION_TIME);
        }
        return ReturnMsg.consSuccess(Constant.SAVE_SUCCESS);
    }

    /**
     * 清空下载集
     *
     * @return com.bcbim.common.response.vo.ReturnMsg
     * @author wxc
     * @date 2021/10/25
     */
    @RequestMapping("/clearDownload")
    public ReturnMsg clearDownload() {
        myReidsService.delete(AuthUtil.getPipeLineDownloadToken());
        myReidsService.delete(AuthUtil.getPipePointDownloadToken());
        return ReturnMsg.consSuccess("清空下载集成功！");
    }

//    /**
//     * 私有方法：添加数据到管线集选
//     *
//     * @param numberList 管线集选编号
//     * @return boolean
//     * @author wxc
//     * @date 2021/10/25
//     */
//    private boolean addDownload(List<Code> numberList) {
//        if (ObjectUtils.isEmpty(numberList)) {
//            return false;
//        }
//        // 取redis中数据集合
//        String tokenDown = Constant.DOWN_TOKEN_PERFIX + AuthUtil.getTokenEncrypt();
//        JSONArray jsonArray = (JSONArray) myReidsService.get(tokenDown);
//        List<Code> cacheList = jsonArray == null ? null : jsonArray.toJavaList(Code.class);
//        List<Code> list;
//        // 如果缓存有数据(去重)，没有数据（让list等于numberList）
//        if (ObjectUtils.isNotEmpty(cacheList)) {
//            list = MyListUtil.removeAll(numberList, cacheList);
//            list.addAll(cacheList);
//        } else {
//            list = numberList;
//        }
//        myReidsService.set(tokenDown, list, Constant.PERMISSION_EXPIRATION_TIME);
//        return true;
//    }

    @RequestMapping("/insertDownload")
    public ReturnMsg insertDownload(String id, String type) {
        try {
            ParamCheck.idCheck(id);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        String pointToken = AuthUtil.getPointToken();
        String lineToken = AuthUtil.getLineToken();
        String pipeLineDownload = AuthUtil.getPipeLineDownloadToken();
        String pipePointDownload = AuthUtil.getPipePointDownloadToken();

        if (type.equals(Constant.PIPELINE_POINT)) {
            JSONArray jsonArray = (JSONArray) myReidsService.get(pointToken);
            List<PipeListVO> pointList = jsonArray == null ? null : jsonArray.toJavaList(PipeListVO.class);
            PipeListVO pipeListVO = pointList.stream().filter(pipeListVO1 -> id.equals(pipeListVO1.getId())).collect(Collectors.toList()).get(0);
            List<PipeListVO> pipePointList = Lists.newArrayList(pipeListVO);

            JSONArray json = (JSONArray) myReidsService.get(pipePointDownload);
            List<PipeListVO> cacheList = json == null ? null : json.toJavaList(PipeListVO.class);
            List<PipeListVO> list = new ArrayList<>();
            if (ObjectUtils.isNotEmpty(cacheList)) {
                list = MyListUtil.removeAll(pipePointList, cacheList);
                list.addAll(cacheList);
            } else {
                list = pipePointList;
            }
            myReidsService.set(pipePointDownload, list, Constant.PERMISSION_EXPIRATION_TIME);
        }
        if (type.equals(Constant.PIPELINE_LINE)) {
            JSONArray jsonArray = (JSONArray) myReidsService.get(lineToken);
            List<PipeListVO> lineList = jsonArray == null ? null : jsonArray.toJavaList(PipeListVO.class);
            PipeListVO pipeLineListVO = lineList.stream().filter(pipeListVO1 -> id.equals(pipeListVO1.getId())).collect(Collectors.toList()).get(0);
            List<PipeListVO> pipeLineList = Lists.newArrayList(pipeLineListVO);

            JSONArray json = (JSONArray) myReidsService.get(pipeLineDownload);
            List<PipeListVO> cacheList = json == null ? null : json.toJavaList(PipeListVO.class);
            List<PipeListVO> list = new ArrayList<>();
            if (ObjectUtils.isNotEmpty(cacheList)) {
                list = MyListUtil.removeAll(pipeLineList, cacheList);
                list.addAll(cacheList);
            } else {
                list = pipeLineList;
            }
            myReidsService.set(pipeLineDownload, list, Constant.PERMISSION_EXPIRATION_TIME);

        }
        return ReturnMsg.consSuccess(Constant.SAVE_SUCCESS);
    }

    /**
     * 加入下载集 (添加的PipeListVO对象到redis)
     *
     * @param type 1-管点 2-管线 3-全部
     * @return ReturnMsg
     * @Author: haomeijie
     * @Date: 2021/10/29 14:55
     */
    @RequestMapping("/batchInsertDownload")
    public ReturnMsg batchInsertDownload(String type) {
        //参数校验
        try {
            ParamCheck.stringCanBeEmpty(type, 1);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        String pointToken = AuthUtil.getPointToken();
        String lineToken = AuthUtil.getLineToken();
        String pipeLineDownload = AuthUtil.getPipeLineDownloadToken();
        String pipePointDownload = AuthUtil.getPipePointDownloadToken();

        List<PipeListVO> pointList = new ArrayList<>();
        List<PipeListVO> lineList = new ArrayList<>();
        if (type.equals(Constant.PIPELINE_POINT) || type.equals(Constant.PIPELINE_ALL)) {
            JSONArray jsonArray = (JSONArray) myReidsService.get(pointToken);
            List<PipeListVO> pipeList = jsonArray == null ? null : jsonArray.toJavaList(PipeListVO.class);

            if (ObjectUtils.isNotEmpty(pipeList)) {
                pointList = pipeList;
            }
        }

        if (type.equals(Constant.PIPELINE_LINE) || type.equals(Constant.PIPELINE_ALL)) {

            JSONArray jsonArray = (JSONArray) myReidsService.get(lineToken);
            List<PipeListVO> pipeList = jsonArray == null ? null : jsonArray.toJavaList(PipeListVO.class);
            if (ObjectUtils.isNotEmpty(pipeList)) {
                lineList = pipeList;
            }
        }

        if (ObjectUtils.isEmpty(pointList) && ObjectUtils.isEmpty(lineList)) {
            return ReturnMsg.consFail("集选暂无数据，请下加入集选！");
        }
        if (ObjectUtils.isNotEmpty(pointList)) {

            JSONArray json = (JSONArray) myReidsService.get(pipePointDownload);
            List<PipeListVO> cacheList = json == null ? null : json.toJavaList(PipeListVO.class);
            List<PipeListVO> list;
            if (ObjectUtils.isNotEmpty(cacheList)) {
                list = MyListUtil.removeAll(pointList, cacheList);
                list.addAll(cacheList);
            } else {
                list = pointList;
            }
            boolean b = myReidsService.set(pipePointDownload, list, Constant.PERMISSION_EXPIRATION_TIME);
            if (b == false) {
                return ReturnMsg.consFail("系统维护，请稍后重试！");
            }
        }
        if (ObjectUtils.isNotEmpty(lineList)) {

            JSONArray json = (JSONArray) myReidsService.get(pipeLineDownload);
            List<PipeListVO> cacheList = json == null ? null : json.toJavaList(PipeListVO.class);
            List<PipeListVO> list;

            if (ObjectUtils.isNotEmpty(cacheList)) {
                list = MyListUtil.removeAll(lineList, cacheList);
                list.addAll(cacheList);
            } else {
                list = lineList;
            }
            boolean b = myReidsService.set(pipeLineDownload, list, Constant.PERMISSION_EXPIRATION_TIME);
            if (b == false) {
                return ReturnMsg.consFail("系统维护，请稍后重试！");
            }
        }
        return ReturnMsg.consSuccess(Constant.SAVE_SUCCESS);
    }

    @RequestMapping("/getPipeList")
    public ReturnPageMsg getPipeList(@RequestParam(required = true) Integer pageNum,
                                     @RequestParam(required = true) Integer pageSize) {

        try {
            ParamCheck.pageNumCheck(pageNum, pageSize);
        } catch (ParamException e) {
            return ReturnPageMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        JSONArray jsonArray = new JSONArray();
        String pipeLineDownload = AuthUtil.getPipeLineDownloadToken();
        String pipePointDownload = AuthUtil.getPipePointDownloadToken();
        JSONArray pointList = (JSONArray) myReidsService.get(pipePointDownload);
        JSONArray lineList = (JSONArray) myReidsService.get(pipeLineDownload);
        if (ObjectUtils.isNotEmpty(pointList)) {
            jsonArray.addAll(pointList);
        }
        if (ObjectUtils.isNotEmpty(lineList)) {
            jsonArray.addAll(lineList);
        }
        if (ObjectUtils.isEmpty(jsonArray)) {
            return ReturnPageMsg.consSuccess(0, new ArrayList<>());
        }
        List<PipeListVO> list = jsonArray.toJavaList(PipeListVO.class);
        List<?> paging = paging(list, pageNum, pageSize);
        return ReturnPageMsg.consSuccess(list.size(), paging);
    }

    private List<?> paging(List<?> list, int pageNum, int pageSize) {
        int total = list.size();
        int start = (pageNum - 1) * pageSize;
        int end = start + pageSize;
        return list.subList(start, total < end ? total : end);
    }

    /**
     * 下载
     *
     * @param checkDraw 是否勾选图纸 0否 1是
     * @return ReturnMsg
     * @Author: haomeijie
     * @Date: 2021/10/29 16:48
     */
    @RequestMapping("/batchDownloadFile")
    @Translate
    public ReturnMsg batchDownloadFile(String checkDraw, HttpServletResponse response) {

        String pipeLineDownload = AuthUtil.getPipeLineDownloadToken();
        String pipePointDownload = AuthUtil.getPipePointDownloadToken();

        JSONArray pointJson = (JSONArray) myReidsService.get(pipePointDownload);
        JSONArray lineJson = (JSONArray) myReidsService.get(pipeLineDownload);
        if (ObjectUtils.isEmpty(pointJson) && ObjectUtils.isEmpty(lineJson)) {
            return ReturnMsg.consFail("下载集数据为空，下载失败！");
        }
        List<PipeListVO> pointList = new ArrayList<>();
        List<PipeListVO> lineList = new ArrayList<>();
        List<PipelineLineVO> pipelineLineList = new ArrayList<>();
        List<PipelinePointVO> pipelinePointList = new ArrayList<>();
        if (ObjectUtils.isNotEmpty(pointJson)) {
            pointList = pointJson.toJavaList(PipeListVO.class);
            for (PipeListVO point : pointList) {
                PipelinePointVO pointVO = pipelinePointServiceImpl.getPipelinePoint(point.getId());
                pipelinePointList.add(pointVO);
            }
            TranslateHandle.translationData(pipelinePointList);
            sendPointExcel(pipelinePointList, response);
        }
        if (ObjectUtils.isNotEmpty(lineJson)) {
            lineList = lineJson.toJavaList(PipeListVO.class);
            for (PipeListVO line : lineList) {
                PipelineLineVO lineVO = pipelineLineServiceImpl.getPipelineLine(line.getId());
                pipelineLineList.add(lineVO);
            }
            TranslateHandle.translationData(pipelineLineList);
            sendLineExcel(pipelineLineList, response);
        }

        if (checkDraw.equals(Constant.CHECK)) {
            List<String> pointIds = new ArrayList<>();
            List<String> lineIds = new ArrayList<>();
            pointList.stream().forEach(point -> {
                String pointId = point.getId();
                pointIds.add(pointId);
            });
            lineList.stream().forEach(line -> {
                String lineId = line.getId();
                lineIds.add(lineId);
            });
            List<FileVO> fileVOList = new ArrayList<>();
            if (ObjectUtils.isNotEmpty(pointIds)) {
                fileVOList.addAll(fileServiceImpl.getFileByPointId(pointIds));
            }
            if (ObjectUtils.isNotEmpty(lineIds)) {
                fileVOList.addAll(fileServiceImpl.getFileByLineId(lineIds));
            }
            fileDownload(fileVOList, response);
        }


        String sourceFilePath = UPLOAD_PATH + File.separator + "temporaryFolder";
        List<File> fileList = Arrays.asList(new File(sourceFilePath).listFiles());
        String filePath = "pipeInformation.zip";
        try {
            FileOutputStream fos = new FileOutputStream(new File(UPLOAD_PATH + File.separator + filePath));
            ZipUtils.toZip(fileList, fos);
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (int i = 0; i < fileList.size(); i++) {
            fileList.get(i).delete();
        }
        new File(sourceFilePath).delete();
        return ReturnMsg.newInstance().setStatus(ReturnStatus.SUCCESS.getCode()).setData("pipeInformation.zip");
    }


    public void sendPointExcel(List<PipelinePointVO> list, HttpServletResponse response) {
        DecimalFormat df = new DecimalFormat("#.00");

        String[] title = {"管井编号", "特征", "附属物", "地面高程", "用途", "井底埋深", "所在道路", "经度", "纬度",
                "埋设年代", "权属单位", "探测日期", "探测单位", "监理单位", "精度级别"};
        String fileName = "管井数据表" + ".xls";
        String sheetName = "管井数据表";

        String[][] content = new String[list.size()][title.length];
        for (int i = 0; i < list.size(); i++) {
            PipelinePointVO p = list.get(i);
            if (StringUtils.isNotEmpty(p.getCode())) {
                content[i][0] = String.valueOf(p.getCode());
            }
            if (StringUtils.isNotEmpty(p.getFeature())) {
                content[i][1] = String.valueOf(p.getFeature());
            }
            if (StringUtils.isNotEmpty(p.getAttachment())) {
                content[i][2] = String.valueOf(p.getAttachment());
            }
            if (StringUtils.isNotEmpty(p.getHigh())) {
                content[i][3] = String.valueOf(p.getHigh());
            }
            if (StringUtils.isNotEmpty(p.getUsesType())) {
                content[i][4] = String.valueOf(p.getUsesType());
            }
            if (StringUtils.isNotEmpty(p.getBury())) {
                content[i][5] = String.valueOf(p.getBury());
            }
            if (StringUtils.isNotEmpty(p.getRoadName())) {
                content[i][6] = String.valueOf(p.getRoadName());
            }
            if (StringUtils.isNotEmpty(p.getLongitude())) {
                content[i][7] = String.valueOf(p.getLongitude());
            }
            if (StringUtils.isNotEmpty(p.getLatitude())) {
                content[i][8] = String.valueOf(p.getLatitude());
            }
            if (StringUtils.isNotEmpty(p.getConstructionTime())) {
                content[i][9] = String.valueOf(p.getConstructionTime());
            }
            if (StringUtils.isNotEmpty(p.getCompany())) {
                content[i][10] = String.valueOf(p.getCompany());
            }
            if (StringUtils.isNotEmpty(p.getSurveyTime())) {
                content[i][11] = String.valueOf(p.getSurveyTime());
            }
            if (StringUtils.isNotEmpty(p.getSurveyUnit())) {
                content[i][12] = String.valueOf(p.getSurveyUnit());
            }
            if (StringUtils.isNotEmpty(p.getSupervisingUnit())) {
                content[i][13] = String.valueOf(p.getSupervisingUnit());
            }
            if (StringUtils.isNotEmpty(p.getAccuracyLevel())) {
                content[i][14] = String.valueOf(p.getAccuracyLevel());
            }
        }

        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);

        this.setResponseHeader(response, fileName);
        File filepath = new File(UPLOAD_PATH + File.separator + "temporaryFolder");
        if (!filepath.exists()) {
            filepath.mkdirs();
        }
        try {
            FileOutputStream output = new FileOutputStream(filepath + File.separator + fileName);
            wb.write(output);//写入磁盘
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void sendLineExcel(List<PipelineLineVO> list, HttpServletResponse response) {
        DecimalFormat df = new DecimalFormat("#.00");

        String[] title = {"管线编号", "起始点编号", "终止点编号", "流向", "用途", "材质", "起始点高程", "终止点高程", "起始点埋深",
                "终止点埋深", "埋设方式", "管径", "埋设年代", "权属单位", "所在道路", "探测日期", "探测单位", "监理单位"};
        String fileName = "管线数据表" + ".xls";
        String sheetName = "管线数据表";

        String[][] content = new String[list.size()][title.length];
        for (int i = 0; i < list.size(); i++) {
            PipelineLineVO l = list.get(i);
            if (StringUtils.isNotEmpty(l.getCode())) {
                content[i][0] = String.valueOf(l.getCode());
            }
            if (StringUtils.isNotEmpty(l.getStartPoint())) {
                content[i][1] = String.valueOf(l.getStartPoint());
            }
            if (StringUtils.isNotEmpty(l.getEndPoint())) {
                content[i][2] = String.valueOf(l.getEndPoint());
            }
            if (StringUtils.isNotEmpty(l.getFlowDirection())) {
                content[i][3] = String.valueOf(l.getFlowDirection());
            }
            if (StringUtils.isNotEmpty(l.getUses())) {
                content[i][4] = String.valueOf(l.getUses());
            }
            if (StringUtils.isNotEmpty(l.getMaterial())) {
                content[i][5] = String.valueOf(l.getMaterial());
            }
            if (StringUtils.isNotEmpty(l.getStartHigh())) {
                content[i][6] = String.valueOf(l.getStartHigh());
            }
            if (StringUtils.isNotEmpty(l.getEndHigh())) {
                content[i][7] = String.valueOf(l.getEndHigh());
            }
            if (StringUtils.isNotEmpty(l.getStartBury())) {
                content[i][8] = String.valueOf(l.getStartBury());
            }
            if (StringUtils.isNotEmpty(l.getEndBury())) {
                content[i][9] = String.valueOf(l.getEndBury());
            }
            if (StringUtils.isNotEmpty(l.getBuryType())) {
                content[i][10] = String.valueOf(l.getBuryType());
            }
            if (ObjectUtils.isNotEmpty(l.getPipeDiameter())) {
                content[i][11] = String.valueOf(l.getPipeDiameter());
            }
            if (StringUtils.isNotEmpty(l.getConstructionTime())) {
                content[i][12] = String.valueOf(l.getConstructionTime());
            }
            if (StringUtils.isNotEmpty(l.getCompany())) {
                content[i][13] = String.valueOf(l.getCompany());
            }
            if (StringUtils.isNotEmpty(l.getRoadName())) {
                content[i][14] = String.valueOf(l.getRoadName());
            }
            if (StringUtils.isNotEmpty(l.getSurveyTime())) {
                content[i][15] = String.valueOf(l.getSurveyTime());
            }
            if (StringUtils.isNotEmpty(l.getSurveyUnit())) {
                content[i][16] = String.valueOf(l.getSurveyUnit());
            }
            if (StringUtils.isNotEmpty(l.getSupervisingUnit())) {
                content[i][17] = String.valueOf(l.getSupervisingUnit());
            }
        }

        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        this.setResponseHeader(response, fileName);
        File filepath = new File(UPLOAD_PATH + File.separator + "temporaryFolder");
        if (!filepath.exists()) {
            filepath.mkdirs();
        }
        try {
            FileOutputStream output = new FileOutputStream(filepath + File.separator + fileName);
            wb.write(output);//写入磁盘
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void fileDownload(List<FileVO> files, HttpServletResponse res) {
        for (FileVO fileVO : files) {
            try {
                String displayName = fileVO.getDisplayName();
                String keepName = UPLOAD_PATH + Constant.PIPE_DRAWING_URL + File.separator + fileVO.getKeepName();
                String type = keepName.substring(keepName.lastIndexOf(".") + 1);
                File file = new File(keepName);
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                byte[] bytes = new byte[bis.available()];
                bis.read(bytes);
                bis.close();
                File filepath = new File(UPLOAD_PATH + File.separator + "temporaryFolder");
                if (!filepath.exists()) {
                    filepath.mkdirs();
                }
                FileOutputStream fileOut = new FileOutputStream(filepath + File.separator + displayName + "." + type);
                BufferedOutputStream bos = new BufferedOutputStream(fileOut);
                bos.write(bytes);

                bos.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
